package com.shuda.business.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuda.business.crm.entity.ChannelSourceEntity;
import com.shuda.business.crm.service.ChannelSourceService;
import com.shuda.common.core.domain.R;
import com.shuda.common.core.domain.PageRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 渠道来源配置Controller
 * @author shuda
 */
@RestController
@RequestMapping("/crm/channel-source")
@Tag(name = "渠道来源配置管理", description = "渠道来源配置相关接口")
@RequiredArgsConstructor
public class ChannelSourceController {

    private final ChannelSourceService channelSourceService;

    /**
     * 分页查询渠道来源配置
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询渠道来源配置", description = "根据条件分页查询渠道来源配置")
    public R<IPage<ChannelSourceEntity>> getChannelSourcePage(
            @Parameter(description = "分页请求参数") PageRequest pageRequest,
            @Parameter(description = "渠道名称") @RequestParam(required = false) String channelName,
            @Parameter(description = "渠道类型") @RequestParam(required = false) String channelType,
            @Parameter(description = "渠道状态") @RequestParam(required = false) String channelStatus,
            @Parameter(description = "负责人姓名") @RequestParam(required = false) String ownerName) {
        
        Page<ChannelSourceEntity> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        IPage<ChannelSourceEntity> result = channelSourceService.getChannelSourcePage(page, channelName, channelType, channelStatus, ownerName);
        return R.ok(result);
    }

    /**
     * 查询活跃的渠道来源列表
     */
    @GetMapping("/active")
    @Operation(summary = "查询活跃的渠道来源列表", description = "查询所有活跃的渠道来源配置")
    public R<List<ChannelSourceEntity>> getActiveChannelSources() {
        List<ChannelSourceEntity> result = channelSourceService.getActiveChannelSources();
        return R.ok(result);
    }

    /**
     * 根据ID获取渠道来源配置
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取渠道来源配置", description = "根据ID获取渠道来源配置详情")
    public R<ChannelSourceEntity> getChannelSourceById(@Parameter(description = "渠道来源ID") @PathVariable Long id) {
        ChannelSourceEntity channelEntity = channelSourceService.getChannelSourceById(id);
        return channelEntity != null ? R.ok(channelEntity) : R.fail("渠道来源配置不存在");
    }

    /**
     * 根据编码获取渠道来源配置
     */
    @GetMapping("/code/{channelCode}")
    @Operation(summary = "根据编码获取渠道来源配置", description = "根据编码获取渠道来源配置")
    public R<ChannelSourceEntity> getChannelSourceByCode(
            @Parameter(description = "渠道编码") @PathVariable String channelCode,
            @Parameter(description = "租户ID") @RequestParam Long tenantId) {
        
        ChannelSourceEntity channelEntity = channelSourceService.getChannelSourceByCode(channelCode, tenantId);
        return channelEntity != null ? R.ok(channelEntity) : R.fail("渠道来源配置不存在");
    }

    /**
     * 创建渠道来源配置
     */
    @PostMapping
    @Operation(summary = "创建渠道来源配置", description = "创建渠道来源配置")
    public R<Boolean> createChannelSource(@RequestBody ChannelSourceEntity channelEntity) {
        boolean result = channelSourceService.createChannelSource(channelEntity);
        return result ? R.ok(true) : R.fail("创建渠道来源配置失败");
    }

    /**
     * 更新渠道来源配置
     */
    @PutMapping
    @Operation(summary = "更新渠道来源配置", description = "更新渠道来源配置")
    public R<Boolean> updateChannelSource(@RequestBody ChannelSourceEntity channelEntity) {
        boolean result = channelSourceService.updateChannelSource(channelEntity);
        return result ? R.ok(true) : R.fail("更新渠道来源配置失败");
    }

    /**
     * 删除渠道来源配置
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除渠道来源配置", description = "根据ID删除渠道来源配置")
    public R<Boolean> deleteChannelSource(@Parameter(description = "渠道来源ID") @PathVariable Long id) {
        boolean result = channelSourceService.deleteChannelSource(id);
        return result ? R.ok(true) : R.fail("删除渠道来源配置失败");
    }

    /**
     * 批量删除渠道来源配置
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除渠道来源配置", description = "批量删除渠道来源配置")
    public R<Boolean> batchDeleteChannelSource(@RequestBody Long[] ids) {
        boolean result = channelSourceService.batchDeleteChannelSource(ids);
        return result ? R.ok(true) : R.fail("批量删除渠道来源配置失败");
    }

    /**
     * 启用渠道来源
     */
    @PostMapping("/enable/{id}")
    @Operation(summary = "启用渠道来源", description = "启用渠道来源配置")
    public R<Boolean> enableChannelSource(@Parameter(description = "渠道来源ID") @PathVariable Long id) {
        boolean result = channelSourceService.enableChannelSource(id);
        return result ? R.ok(true) : R.fail("启用渠道来源失败");
    }

    /**
     * 禁用渠道来源
     */
    @PostMapping("/disable/{id}")
    @Operation(summary = "禁用渠道来源", description = "禁用渠道来源配置")
    public R<Boolean> disableChannelSource(@Parameter(description = "渠道来源ID") @PathVariable Long id) {
        boolean result = channelSourceService.disableChannelSource(id);
        return result ? R.ok(true) : R.fail("禁用渠道来源失败");
    }

    /**
     * 更新排序
     */
    @PutMapping("/sort/{id}")
    @Operation(summary = "更新排序", description = "更新渠道来源排序")
    public R<Boolean> updateSortOrder(
            @Parameter(description = "渠道来源ID") @PathVariable Long id,
            @Parameter(description = "排序号") @RequestParam Integer sortOrder) {
        
        boolean result = channelSourceService.updateSortOrder(id, sortOrder);
        return result ? R.ok(true) : R.fail("更新排序失败");
    }

    /**
     * 更新负责人
     */
    @PutMapping("/owner/{id}")
    @Operation(summary = "更新负责人", description = "更新渠道来源负责人")
    public R<Boolean> updateOwner(
            @Parameter(description = "渠道来源ID") @PathVariable Long id,
            @Parameter(description = "负责人ID") @RequestParam Long ownerId,
            @Parameter(description = "负责人姓名") @RequestParam String ownerName) {
        
        boolean result = channelSourceService.updateOwner(id, ownerId, ownerName);
        return result ? R.ok(true) : R.fail("更新负责人失败");
    }
}